package com.ity.teamanagement.service.Impl;

import com.ity.teamanagement.code.RotationChartCode;
import com.ity.teamanagement.entity.RotationChart;
import com.ity.teamanagement.entity.inputParam.AddRotationChartParam;
import com.ity.teamanagement.entity.inputParam.DeleteRotationChartParam;
import com.ity.teamanagement.entity.inputParam.SelectRotationChartParam;
import com.ity.teamanagement.entity.inputParam.UpdateRotationChartParam;
import com.ity.teamanagement.entity.outputResult.RotationChartListVo;
import com.ity.teamanagement.exception.ExceptionUtils;
import com.ity.teamanagement.mapper.RotationChartMapper;
import com.ity.teamanagement.result.Result;
import com.ity.teamanagement.service.RotationChartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author coderFu
 * @create 2021/11/10
 */
@Service
public class RotationChartServiceImpl implements RotationChartService {

    @Autowired
    private RotationChartMapper rotationChartMapper;


    @Override
    public Result addRotationChart(AddRotationChartParam addRotationChartParam) {
        RotationChart rotationChart = new RotationChart();
        //查询最大的id值
        Integer maxId = rotationChartMapper.getRotationChartMaxId();
        if (maxId == null){
            maxId =0;
        }
        //判断是否设置顺序
        BeanUtils.copyProperties(addRotationChartParam, rotationChart);
        //判断是否填写顺序
        Integer orders = addRotationChartParam.getOrders();
        if (orders == null) {
            //没有填写顺序，此时的orders和最大的id+1
            rotationChart.setOrders(maxId + 1);
        } else {
            if (orders > maxId) {
                rotationChart.setOrders(maxId + 1);
            } else {
                //填写了顺序,判断顺序是否存在
                RotationChart rotationChart1 = rotationChartMapper.getRotationChartByOrders(orders);
                //防止特殊删除情况，不等于null的时候替换，不然不替换
                if (rotationChart1 != null) {
                    //存在，将原顺序设置为最大的
                    rotationChart1.setOrders(maxId + 1);
                    Integer integer = rotationChartMapper.updateRotationChart(rotationChart1);
                    if (integer < 1) {
                        ExceptionUtils.error(RotationChartCode.ROTATION_CHART_UPDATE_FAIL);
                    }
                }
            }
        }
        rotationChart.setCreateTime(new Date());
        Integer integer = rotationChartMapper.addRotationChart(rotationChart);
        if (integer < 1) {
            ExceptionUtils.error(RotationChartCode.ROTATION_CHART_ADD_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result updateRotationChart(UpdateRotationChartParam updateRotationChartParam) {
        //原来的轮播图信息
        RotationChart sourceRotationChart = rotationChartMapper.getRotationChartById(updateRotationChartParam.getId());
        RotationChart rotationChart = new RotationChart();
        Integer maxId = rotationChartMapper.getRotationChartMaxId();
        BeanUtils.copyProperties(updateRotationChartParam, rotationChart);
        if (updateRotationChartParam.getOrders() >= maxId) {
            //如果设置的顺序大于当前最大的值，则使用之前的值
            rotationChart.setOrders(sourceRotationChart.getOrders());
        } else {
            //判断当前orders是否被其它人使用
            RotationChart rotationChart1 = rotationChartMapper.getRotationChartByOrders(updateRotationChartParam.getOrders());
           //防止之前的顺序已经被删了
            if (rotationChart1 != null) {
                //已经被使用,交换顺序
                rotationChart1.setOrders(sourceRotationChart.getOrders());
                Integer integer = rotationChartMapper.updateRotationChart(rotationChart1);
                if (integer < 1) {
                    ExceptionUtils.error(RotationChartCode.ROTATION_CHART_UPDATE_FAIL);
                }
            }
        }
        //更新操作
        rotationChart.setUpdateTime(new Date());
        Integer integer = rotationChartMapper.updateRotationChart(rotationChart);
        if (integer < 1) {
            ExceptionUtils.error(RotationChartCode.ROTATION_CHART_UPDATE_FAIL);
        }
        return Result.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteRotationChart(DeleteRotationChartParam deleteRotationChartParam) {
        RotationChart rotationChart = rotationChartMapper.getRotationChartById(deleteRotationChartParam.getId());
        // 查询该顺序之后的轮播图依次修改展示顺序

        Integer integer = rotationChartMapper.deleteRotationChart(deleteRotationChartParam.getId());
        if (integer <1){
            ExceptionUtils.error(RotationChartCode.ROTATION_CHART_DELETE_FAIL);
        }
        List<RotationChart> rotationChartList =  rotationChartMapper.getRotationChartListByOrder(rotationChart.getOrders());
        // 修改后续轮播图的顺序
        for (int i = 0 ,start = rotationChart.getOrders(); i <rotationChartList.size(); i++ ,start++) {
            RotationChart rotationChart1 = rotationChartList.get(i);
            rotationChart1.setOrders(start);
            rotationChartMapper.updateRotationChart(rotationChart1);
        }
        return Result.success();
    }

    @Override
    public Result getRotationChartDetailsById(SelectRotationChartParam selectRotationChartParam) {
        RotationChart rotationChart = rotationChartMapper.getRotationChartDetailsById(selectRotationChartParam.getId());
        return Result.success(rotationChart);
    }

    @Override
    public Result getRotationChartList() {
        List<RotationChartListVo> rotationChartListVos =  rotationChartMapper.getRotationChartList();
        return Result.success(rotationChartListVos);
    }
}
